package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.ValuationDetailData;
import com.jzg.jcptadmin.viewinterface.ValuationDetailInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/1/15.
 */
public class ValuationDetailPresenter extends BasePresenter<ValuationDetailInterface> {
    private DataManager dataManager;
    private ValuationDetailInterface mView;
    private Subscription mSubscription;

    public ValuationDetailPresenter(DataManager dataManager) {
        this.dataManager = dataManager;

    }

    @Override
    public void attachView(ValuationDetailInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void ValuationDetailData(String userid, String Styleid, String carId) {
        mSubscription = dataManager.LoadValuationDetailData(userid, Styleid, carId).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<ValuationDetailData>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                if (e != null&& mView!=null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(ValuationDetailData valuationDetailData) {
                if (valuationDetailData.getStatus() == Constant.SUCCESS)
                    mView.showData(valuationDetailData);
                else
                    mView.showError(valuationDetailData.getMsg());
            }
        });

    }


}
